FriendScene={}
FriendSceneFunc={}
FriendSceneFunc.__index = FriendSceneFunc
setmetatable(FriendScene, FriendSceneFunc)

FriendSceneFunc.getNode = function()
  local l = {}
  setmetatable(l, FriendSceneFunc)
  l.node = CCBReader:load("FriendScene.ccbi")
  FriendScene.instance = l
  l:init()
  return l
end

FriendSceneFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(FriendScene.getNode().node)
  return sc
end

FriendSceneFunc.init = function(self)
  setObjectExtTable(self.node:getChildByTag(1111), self)
  self.menu = self.node:getChildByTag(1001)
  self.radioMenu = RadioMenu:create(self.menu, "menuItemChanged", self)
  self.radioMenu:setMenuState(0)
end

FriendSceneFunc.menuItemChanged = function(self, oldIndex, newIndex)
  self:hideAll()
  if newIndex==0 then
    --setTouchEnabledAll(self.menu, false)
    LoadScene:showLoadingScene()
    NetworkRequest.getFriendList("onGetFriendListComplete", self)
  elseif newIndex==1 then
    --setTouchEnabledAll(self.menu, false)
    LoadScene:showLoadingScene()
    NetworkRequest.viewFriends("onGetUsersComplete", self)
  elseif newIndex==2 then
    --local l = self.node:getChildByTag(2006)
    --l:setVisible(true)
    --setTouchEnabledAll(self.menu, false)
    LoadScene:showLoadingScene()
    NetworkRequest.viewInvitationFriends("onGetInviteFriendsComplete", self)
  elseif newIndex==3 then
    self:showFindLayer()
  elseif newIndex==4 then
    local l = self.node:getChildByTag(2005)
    l:setVisible(true)
    setTouchEnabledAll(l, true)
    TypeCast(l:getChildByTag(1011), "CCLabelTTF"):setString(string.format("您的ID：%d", Player.id))
  end
end

FriendSceneFunc.hideAll = function(self)
  self.node:getChildByTag(2001):setVisible(false)
  setTouchEnabledAll(self.node:getChildByTag(2001), false)
  self.node:getChildByTag(2002):setVisible(false)
  setTouchEnabledAll(self.node:getChildByTag(2002), false)
  self.node:getChildByTag(2003):setVisible(false)
  setTouchEnabledAll(self.node:getChildByTag(2003), false)
  self.node:getChildByTag(2004):setVisible(false)
  setTouchEnabledAll(self.node:getChildByTag(2004), false)
  self.node:getChildByTag(2005):setVisible(false)
  setTouchEnabledAll(self.node:getChildByTag(2005), false)
  self.node:getChildByTag(2006):setVisible(false)
  setTouchEnabledAll(self.node:getChildByTag(2006), false)
  if self.idEditBox~=nil then
    TypeCast(self.idEditBox, "CCNode"):getParent():removeChild(self.idEditBox, true)
    self.idEditBox = nil
  end
end

FriendSceneFunc.onGetUsersComplete = function(self, data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    self:showAddFriendLayer(res)
  else
    showMessage(res.msg, ccc3(255,0,0))
  end
end
FriendSceneFunc.onGetInviteFriendsComplete = function(self, data)
  print("FriendSceneFunc.onGetInviteFriendsComplete", data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    self:showInvitationLayer(res)
  else
    --showMessage(res.msg, ccc3(255,0,0))
    setTouchEnabledAll(self.node:getChildByTag(2006), true)
    setTouchEnabledAll(self.menu, true) 
  end
end
FriendSceneFunc.showFindLayer = function(self)
  local l = self.node:getChildByTag(2004)
  l:setVisible(true)
  setTouchEnabledAll(l, true)
  
  local b = TypeCast(l:getChildByTag(1020), "CCButton")
  setObjectExtTable(b, self)
  --b:setEnabled(false)
  self.findButton = b
  
  local resLayer = l:getChildByTag(1001)
  resLayer:setVisible(false)
  setTouchEnabledAll(resLayer, false)
  setObjectExtTable(resLayer:getChildByTag(1020), self)
  self.resLayer = resLayer

  local editBox = CCEditBox:create(CCSizeMake(320, 54), "editbox_bk1.png", "点击输入")
  setObjectExtTable(editBox, self)
  editBox:setPosition(ccp(670, 458))
  l:addChild(editBox)
  self.idEditBox = editBox
end
FriendSceneFunc.onEditBoxReturn = function(self, node, event)
  --[[
  local text = self.idEditBox:getText()
  if text~="" then
    self.findButton:setEnabled(true)
  end
  ]]
end
FriendSceneFunc.onFindButton = function(self, node, event)
  local text = self.idEditBox:getText()
  NetworkRequest.findFriend(text, "onFindComplete", self)
  LoadScene:showLoadingScene()
end
FriendSceneFunc.onFindComplete = function(self, data)
  print("FriendSceneFunc.showInviteLayer", data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    addSpriteFromBK(self.resLayer, 1001, nameFromPath(res.data.headpic))
    TypeCast(self.resLayer:getChildByTag(1011), "CCLabelTTF"):setString(res.data.name)
    self.resLayer:setVisible(true)
    setTouchEnabledAll(self.resLayer, true)
    self.foundFriend = res.data.id
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
end
FriendSceneFunc.onFindInviteButton = function(self, node, event)
  NetworkRequest.inviteFriends({[1]=self.foundFriend}, "onInviteComplete", self)
  LoadScene:showLoadingScene()
end

FriendSceneFunc.showAddFriendLayer = function(self, res)
  self.node:getChildByTag(2002):setVisible(true)
  setTouchEnabledAll(self.node:getChildByTag(2002), true)
  setTouchEnabledAll(self.menu, true)
  LoadScene:removeLoadingScene()
  
  self.users = {}
  local items = CCArray:create()
  for k,v in ipairs(res.gameUsers) do
    local item = CCBReader:load("FriendItem.ccbi")
    addSpriteFromBK(item, 1001, nameFromPath(v.headpic))
    TypeCast(item:getChildByTag(1010), "CCLabelTTF"):setString(v.name)
    TypeCast(item:getChildByTag(1011), "CCLabelTTF"):setString(string.format("Lv%d", v.level))
    local b = item:getChildByTag(1020)
    setObjectExtTable(b, self)
    self.users[b] = v
    item:setContentSize(CCSizeMake(450, 150))
    items:addObject(item)
  end
  if self.inviteTable~=nil then
    self.inviteTable:setDatas(items)
    return
  end 
  local tableview = CCTableView:create(1, CCSizeMake(550, 360), items)
  tableview:setPosition(ccp(340, 100))
  self.node:getChildByTag(2002):addChild(tableview)
  --self.heroTable = tableview 
  self.inviteTable = tableview
end

FriendSceneFunc.showInvitationLayer = function(self, res)
  self.node:getChildByTag(2006):setVisible(true)
  setTouchEnabledAll(self.node:getChildByTag(2006), true)
  --setTouchEnabledAll(self.menu, true)
  --LoadScene:removeLoadingScene()
  if self.invitationTable~=nil then
    self.node:getChildByTag(2006):removeChild(self.invitationTable, true)
    self.invitationTable = nil
  end
  if res.gameUsers==nil or #res.gameUsers<=0 then
    return
  end
  self.invitationUsers = {}
  self.invitationUsers1 = {}
  local items = CCArray:create()
  for k,v in ipairs(res.gameUsers) do
    local item = CCBReader:load("FriendItem1.ccbi")
    addSpriteFromBK(item, 1001, nameFromPath(v.headpic))
    TypeCast(item:getChildByTag(1010), "CCLabelTTF"):setString(v.name)
    TypeCast(item:getChildByTag(1011), "CCLabelTTF"):setString(string.format("Lv%d", v.level))
    local b = item:getChildByTag(1020)
    setObjectExtTable(b, self)
    self.invitationUsers[b] = v
    b = item:getChildByTag(1021)
    setObjectExtTable(b, self)
    self.invitationUsers1[b] = v
    item:setContentSize(CCSizeMake(450, 150))
    items:addObject(item)
  end
   
  local tableview = CCTableView:create(1, CCSizeMake(550, 360), items)
  tableview:setPosition(ccp(340, 100))
  self.node:getChildByTag(2006):addChild(tableview)
  self.invitationTable = tableview
end

FriendSceneFunc.onInviteButton = function(self, node, event)
  local u = self.users[node] 
  NetworkRequest.inviteFriends({[1]=u.id}, "onInviteComplete", self)
  LoadScene:showLoadingScene()
end
FriendSceneFunc.onInviteComplete = function(self, data)
  LoadScene:removeLoadingScene()
  print("FriendSceneFunc.onInviteComplete", data)
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    showMessage("已发送邀请", ccc3(0,255,0))
  else
    showMessage(res.msg, ccc3(255,0,0))
  end
end
FriendSceneFunc.onGetFriendListComplete = function(self, data)
  LoadScene:removeLoadingScene()
  self.node:getChildByTag(2003):setVisible(true)
  setTouchEnabledAll(self.node:getChildByTag(2003), true)
  setTouchEnabledAll(self.menu, true)

  print("FriendSceneFunc.onGetFriendListComplete", data)
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    if res.ret==0 then
      self:showFriendListLayer(res)
    end
  else
    showMessage(res.msg, ccc3(255,0,0))
  end
end
FriendSceneFunc.showFriendListLayer = function(self, res)
  if self.friendsTable~=nil then
    self.node:getChildByTag(2003):removeChild(self.friendsTable, true)
    self.friendsTable = nil
  end
  if res.friends==nil or #res.friends<=0 then
    return
  end 
  self.friendsList = {}
  local items = CCArray:create()
  for k,v in ipairs(res.friends) do
    local item = CCBReader:load("FriendItem2.ccbi")
    addSpriteFromBK(item, 1001, nameFromPath(v.headpic))
    TypeCast(item:getChildByTag(1010), "CCLabelTTF"):setString(v.name)
    TypeCast(item:getChildByTag(1011), "CCLabelTTF"):setString(string.format("Lv%d", v.level))
    local b = item:getChildByTag(1021)
    setObjectExtTable(b, self)
    self.friendsList[b] = v
    item:setContentSize(CCSizeMake(450, 150))
    items:addObject(item)
  end
  local tableview = CCTableView:create(1, CCSizeMake(550, 360), items)
  tableview:setPosition(ccp(340, 100))
  self.node:getChildByTag(2003):addChild(tableview)
  self.friendsTable = tableview
end

FriendSceneFunc.onAcceptButton = function(self, node, event)
  local u = self.invitationUsers[node] 
  NetworkRequest.handleInvitation(u.uid, "true", "onHandleInvitationComplete", self)
  LoadScene:showLoadingScene()
end
FriendSceneFunc.onRemoveButton = function(self, node, event)
  local u = self.invitationUsers1[node] 
  NetworkRequest.handleInvitation(u.uid, "false", "onHandleInvitationComplete", self)
  LoadScene:showLoadingScene()
end
FriendSceneFunc.onHandleInvitationComplete = function(self, data)
  print("FriendSceneFunc.onHandleInvitationComplete", data)
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    --LoadScene:showLoadingScene()
    NetworkRequest.viewInvitationFriends("onGetInviteFriendsComplete", self)
  else
    LoadScene:removeLoadingScene()
    showMessage(res.msg, ccc3(255,0,0))
  end
end
FriendSceneFunc.onClose = function(self, node, event)
  MainScene.scene:removeChild(node:getParent(), true)
  setTouchEnabledAll(MainScene.node, true)
  MainScene.setState()
  FriendScene.instance = nil
end
